package org.voyager.service.admin.ad.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.voyager.common.beans.JsonResult;
import org.voyager.common.utils.UUIDUtil;
import org.voyager.service.admin.ad.dao.AdResMapper;
import org.voyager.service.admin.ad.dao.AdResourceMapper;
import org.voyager.service.admin.ad.po.AdResource;
import org.voyager.service.admin.ad.service.AdResourceService;
import org.voyager.service.admin.ad.vo.AdResourceVo;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class AdResourceServiceImpl implements AdResourceService {

	@Autowired
	private AdResourceMapper adResourceMapper;
	@Autowired
	private AdResMapper adResMapper;

	@Override
	public JsonResult add(AdResource adResource) {
		adResource.setId(UUIDUtil.randomUUID8());
		adResource.setCreateTime(new Date());
		adResource.setUpdateTime(new Date());
		adResource.setStatus((adResource.getStatus()!=null? adResource.getStatus() : 1));
		if (adResourceMapper.insertSelective(adResource) == 1) {
			return JsonResult.ok();
		} else {
			return JsonResult.error("添加失败");
		}
	}

	@Override
	public JsonResult del(String id) {
		if (adResourceMapper.deleteByPrimaryKey(id) == 1) {
			return JsonResult.ok();
		} else {
			return JsonResult.error("删除失败");
		}
	}

	@Override
	public JsonResult pageList(AdResourceVo adResourceVo) {
		Integer pageNo = adResourceVo.getPageNo()!=null ? adResourceVo.getPageNo() : 1;
		Integer pageSize = adResourceVo.getPageSize() !=null ? adResourceVo.getPageSize() : 1000;
		Page<AdResourceVo> page = PageHelper.startPage(pageNo, pageSize).doSelectPage(() -> adResMapper.selectByCondition(adResourceVo));
		Map<Object, Object> map = new HashMap<>();
		map.put("pages", page.getPages()); // 总页数
		map.put("total", page.getTotal()); // 总记录数
		map.put("list", page.getResult()); // list
		return JsonResult.ok().put("data", map);
	}
	
}
